import { action, observable, computed } from 'mobx';
import { message } from 'antd';
import { WeaTableNew } from 'comsMobx';
import * as API from "../../apis/performance/accessResultListApi";
import { idsAndNamesToArr } from '../../util/Util'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-考核成绩
 * jiansong 2019/6/11
 */
const { TableStore } = WeaTableNew;
export default class AccessAuditListStore {

    @observable loading = true; //加载中
    @observable exportLoading = false; //导出加载中
    @observable sendListloading = true; //提醒列表加载中
    @observable tableStore = new TableStore(); // new table
    @observable data = {};
    @observable showSearchAd = false; // 高级搜索面板显示
    @observable sendSelectedRowKeys = []; // 发送提醒选择框
    topTab = [];

    @computed get hrmData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.hrmids, this.queryParams.hrmnames);
    }
    @computed get subcompanyData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.subcompanyids, this.queryParams.subcompanynames);
    }
    @computed get departmentData() { //部门回显数据
        return idsAndNamesToArr(this.queryParams.departmentids, this.queryParams.departmentnames);
    }

    @observable statusData = [];
    @observable includesubData = [];
    @observable includedeptData = [];
    @observable isEnableCycle = { //考核成绩是否启用考核周期
        isfyear: 1, //年度
        ishyear: 1, //半年
        isquarter: 1, //季度
        ismonth: 1, //月度
    };
    yearDatas = [];//年集合
    @observable monthdata = [];//月集合
    @observable nowmonth = "";//当前月
    @observable seasondata = [];//季度集合
    @observable nowseason = "";  //当前季度
    @observable cansend = false;  //是否可以发送提醒
    @observable showsend = false;  //是否显示发送提醒页面
    titlename = "";
    @observable queryParams = { //查询条件
        year: "",//年份
        type1: null, //类型
        type2: null,  //月份或季度
        status: null, //状态
        hrmids: null, //人员
        hrmnames: null, //人员
        isreset: null,//是否有更新
        minresult: null, //成绩最低
        maxresult: null, //成绩最高
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: null, //分部
        subcompanynames: null, //分部
        includedept: "3", //是否包含部门，默认含子部门
        departmentids: null, //部门
        departmentnames: null, //部门
    };

    @observable sendParams = { //发送提醒查询条件
        lastname: '', //人员名称

        //分页参数
        pageSize: 10, //每页显示数量
        current: 1, //当前页数
    }

    @observable sendResult = { //发送提醒请求结果
        data: [],
        total: 0,
    }

    @action('初始化多语言')
    initLabel = () => {
        this.statusData = [
            { key: '', showname: "" },
            { key: "0", showname: getLabel(508778, '考核中') },
            { key: "1", showname: getLabel('19134', '审批中') },
            { key: "3", showname: getLabel('23774', '已完成') },
            { key: "4", showname: getLabel('1979', '未开始') },
            { key: "5", showname: getLabel('382750', '已过期') },
            { key: "-1", showname: getLabel('508823', '无方案或无数据') },
        ];
        this.includesubData = [
            { key: "3", showname: getLabel('508780', '含子分部') },
            { key: "1", showname: getLabel('18919', '仅本分部') },
            { key: "2", showname: getLabel('18920', '仅子分部') },
        ];
        this.includedeptData = [
            { key: "3", showname: getLabel('508784', '含子部门') },
            { key: "1", showname: getLabel('18916', '仅本部门') },
            { key: "2", showname: getLabel('18917', '仅子部门') },
        ];
        let mdatas = [];
        let sdatas = [];
        mdatas.push({ label: '1', value: getLabel(25825, '1月') });
        mdatas.push({ label: '2', value: getLabel(25826, '2月') });
        mdatas.push({ label: '3', value: getLabel(25827, '3月') });
        mdatas.push({ label: '4', value: getLabel(25828, '4月') });
        mdatas.push({ label: '5', value: getLabel(25829, '5月') });
        mdatas.push({ label: '6', value: getLabel(25830, '6月') });
        mdatas.push({ label: '7', value: getLabel(25831, '7月') });
        mdatas.push({ label: '8', value: getLabel(25832, '8月') });
        mdatas.push({ label: '9', value: getLabel(25833, '9月') });
        mdatas.push({ label: '10', value: getLabel(25834, '10月') });
        mdatas.push({ label: '11', value: getLabel(25835, '11月') });
        mdatas.push({ label: '12', value: getLabel(25836, '12月') });
        sdatas.push({ label: '1', value: getLabel(509092, '1季度') });
        sdatas.push({ label: '2', value: getLabel(509093, '2季度') });
        sdatas.push({ label: '3', value: getLabel(509094, '3季度') });
        sdatas.push({ label: '4', value: getLabel(509095, '4季度') });
        this.monthdata = mdatas;
        this.seasondata = sdatas;
    }

    @action("初始化操作")
    doInit = () => {
        this.queryParams = {
            year: "",//年份
            type1: null, //类型
            type2: null,  //月份或季度
            status: null, //状态
            hrmids: null, //人员
            hrmnames: null, //人员
            isreset: null,//是否有更新
            minresult: null, //成绩最低
            maxresult: null, //成绩最高
            includesub: "3", //是否包含分部，默认含子分部
            subcompanyids: null, //分部
            subcompanynames: null, //分部
            includedept: "3", //是否包含部门，默认含子部门
            departmentids: null, //部门
            departmentnames: null, //部门
        };
        this.getaccessAssessmentCycleData();
    };

    @action("查询条件改变")
    onSearchChange = (val, type) => {
        this.queryParams[type] = val
    };

    @action("发送提醒查询条件改变")
    onSendParamsChange = (params) => {
        for (let key in params) {
            this.sendParams[key] = params[key];
        }
    };

    @action("更多查询条件改变")
    onMoreSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    };

    @action("更改发送提醒页面是否显示")
    changeSend = () => {
        this.showsend = !this.showsend;
        if (this.showsend) {
            //若是显示dialog，则请求最新数据
            this.getSendListData();
        } else {
            //若不显示则清空选中人员
            this.sendSelectedRowKeys = [];
            this.sendParams = {
                lastname: '', //人员名称

                //分页参数
                pageSize: 10, //每页显示数量
                current: 1, //当前页数
            }
        }
    };

    @action("考核成绩是否启用考核周期")
    getaccessAssessmentCycleData = () => {
        this.loading = true;
        API.accessAssessmentCycleApi().then(action(res => {
            if (res.api_status) {
                this.isEnableCycle = res;
                this.topTab = res.stabList;
                if (this.topTab && this.topTab.length > 0) {
                    this.setInitData();
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }))
    };

    @action("初始化数据")
    setInitData() {
        //获取当前年份
        let date = new Date();
        let fullYear = date.getFullYear();
        let ydatas = [];
        for (let i =parseInt(fullYear)+1; i > 2012; i--) {
            ydatas.push({ key: i, showname: i, selected: false });
        }
        this.queryParams.year = fullYear.toString();
        this.yearDatas = ydatas;
        if (this.isEnableCycle.ismonth) {
            this.queryParams.type1 = "1";
        } else if (this.isEnableCycle.isquarter) {
            this.queryParams.type1 = "2";
        } else if (this.isEnableCycle.ishyear) {
            this.queryParams.type1 = "3";
        } else if (this.isEnableCycle.isfyear) {
            this.queryParams.type1 = "4";
        }
        this.getAccessResultListData();
    };

    @action("考核成绩")
    getAccessResultListData = () => {
        this.loading = true;
        API.accessResultListApi(this.queryParams).then(action(res => {
            this.queryParams.year = res.year;
            this.queryParams.type1 = res.type1;
            this.queryParams.type2 = res.type2;
            if (res.type1 == 1) {
                this.nowmonth = res.type2;
            } else if (res.type1 == 2) {
                this.nowseason = res.type2;
            }
            this.titlename = res.titlename;
            this.cansend = res.cansend;
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'));
            }
            this.loading = false;
        }))
    };

    @action("考核成绩导出")
    getAccessResultExportData = () => {
        this.exportLoading = true;
        API.accessResultExportApi(this.queryParams).then(action((data) => {
            if (data.url) {
                window.location.href = `${location.origin}${data.url}`
            }
            this.exportLoading = false;
        }))
    };

    @action("切换高级搜索框")
    setShowSearchAd = bool => this.showSearchAd = bool;

    @action("重置查询条件")
    resetSearchAd = () => {
        //查询条件
        this.queryParams = {
            ...this.queryParams,
            status: null, //状态
            hrmids: null, //人员
            hrmnames: null, //人员
            isreset: null,//是否有更新
            minresult: null, //成绩最低
            maxresult: null, //成绩最高
            includesub: "3", //是否包含分部，默认含子分部
            subcompanyids: null, //分部
            subcompanynames: null, //分部
            includedept: "3", //是否包含分部，默认含子部门
            departmentids: null, //部门
            departmentnames: null, //部门
        };
    };

    @action("搜索")
    advanceSearch = () => {
        this.setShowSearchAd(false);
        this.getAccessResultListData();
    };

    @action('发送提醒选中行改变')
    onSendListSelectChange = (selectedRowKeys, selectedRowValues) => {
        this.sendSelectedRowKeys = selectedRowKeys;
    }

    @action("获取发送提醒人员数据")
    getSendListData = () => {
        this.sendListloading = true;
        let params = {
            ...this.sendParams,
            type: this.queryParams.type1
        }
        API.accessResultSendListApi(params).then(action((res) => {
            if (res.api_status) {
                this.sendResult = res;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'));
            }
            this.sendListloading = false;
        }))
    }

    @action("发送提醒")
    remind = (content) => {
        let params = {
            content: content,
            ids: this.sendSelectedRowKeys
        }
        API.accessResultSendRemindApi(params).then(action((res) => {
            if (res.api_status) {
                //操作成功则清空选中人员
                this.sendSelectedRowKeys = [];
                message.success(getLabel('30700', '操作成功'));
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'));
            }
        }))
    }

}